home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7856 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.8 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: 27 Feb 1996 15:27:04 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h0408INNe3q@anvil.ugrad.cs.ubc.ca>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <1996Feb26.211807.28858@isac.hces.com>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <1996Feb26.211807.28858@isac.hces.com>,
  12. Greg Goodrich <gg@isac.hces.com> wrote:
  13. >Abu Wawda (wawda@alcor.usc.edu) wrote:
  14. >: I'm having trouble understanding what the address of a static array
  15. >: is. For example, if I declare a variable called myarray as:
  16. >:     char myarray[10];
  17. >: then what could &myarray possibly mean? myarray is not a pointer, so
  18. >: &myarray could not possibly be the address of the variable myarray
  19. >: (like it would be if I did char* myarray and then asked for &myarray).
  20. >
  21. >: Functions such as scanf() allow the following:
  22. >
  23. >:     char myarray[10];
  24. >
  25. >:     scanf("%s",&myarray);
  26. >
  27. >: but I don't understand what scanf() could possibly be taking in the
  28. >: second parameter. It can't be: char** since myarray is not a
  29. >: pointer. I CAN understand how the following would work:
  30. >
  31. >This is because C treats the occurrence of array names as the address of
  32. >the array.  Therefore the following are equivalent:
  33.  
  34. ... you mean, the address of the first element, right?
  35.  
  36. >
  37. >    scanf("%s", myarray);
  38. >    scanf("%s", &myarray);
  39. >
  40. >The thing is, the address is implied when you use an array name.  This
  41. >is not so for pointers.  I am not sure why C was incorporated in this
  42. >way.  In my humble opinion, it would make it easier and clearer to force
  43. >the programmer to put the & in front of array names, the same as you
  44. >have to do for any other storage class.
  45.  
  46. No. The use of & in front of array names is perfectly consistent with its use
  47. with other objects. An & in front of a structure name gives you a value
  48. whose type is a pointer to that structure.  An & in front of a long variable
  49. gives you a pointer to long.  An & in front of a function name gives you a
  50. pointer to that function type.  And an & in front of an array name gives you a
  51. pointer to that array (rather than a pointer to the element type).
  52.  
  53. Where the treatment of arrays (and functions) differs is when you state the
  54. name by itself in an expression. Unfortunately, C adopted this clever little
  55. rule that if you name an array, the resulting expression is a pointer to the
  56. first element, rather than a reference to the array. If this little unfortunate
  57. semantic rule did not exist, the name of an array could be treated as an
  58. lvalue, and array assignment would be a possibility. This is one operation in
  59. which C gives you two ways. You can say 'array_name', or you can say
  60. '&array_name[0]'. However, in structures, there is no equivalence between
  61. 'struct_name' and '&struct_name.first_member'.
  62.  
  63. The unfortunate collapsing rule is directly related to the equivalence of
  64. E1[E2] and *((E1) + (E2)), which requires that the E1 array designator (or
  65. perhaps E2) collapse into a pointer. This silly equivalence, incidentally,
  66. allows the expressions a[i], i[a], to be equivalent, provided a is an  array
  67. (or pointer) and i an integral type (or vice versa :).  The [] postfix operator
  68. could easily have been adopted as special, just like the -> or . operator, and
  69. distinct from pointer dereferencing.  Then a restriction that the only way to
  70. get the pointer to element 0 of an array would be with the & operator would
  71. allow the same privileges for arrays that are shared by structures (passing by
  72. value, copy by assigning, returning from functions for compatible arrays).
  73. Note that [] could still be usable with pointers---it would be overloaded.
  74.  
  75. That would not be the C language anymore, though. C is cool the way it is!
  76. -- 
  77.  
  78.  
  79.  
  80. -- 
  81.  
  82.